using MicroMsg.Manager;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public class ChatInputTextBox : ContentControl
	{
		public delegate void ChatTxtBoxAtMemberCallBack(object sender, object object_0);
		private const string TAG = "ChatInputTextBox";
		public static readonly DependencyProperty HintTextProperty = DependencyProperty.Register("HintText", typeof(string), typeof(ChatInputTextBox), new PropertyMetadata("", null));
		public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ChatInputTextBox), new PropertyMetadata("", null));
		public static readonly DependencyProperty HintForegroundProperty = DependencyProperty.Register("HintForeground", typeof(Brush), typeof(ChatInputTextBox), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)), null));
		private TextBlock hintTxt;
		public TextBox realBox;
		private ScrollViewer scrollViewer;
		public bool bEnableAtNotify;
		private bool bBackKeyDown = true;
		private List<string> strNickNameList = new List<string>();
		public bool mBShowHintWhenEmpty;
		private bool isFocus;
		private bool _contentLoaded;
		public event ChatInputTextBox.ChatTxtBoxAtMemberCallBack ChatTxtBoxAtMember;
		public string HintText
		{
			get
			{
				return (string)base.GetValue(ChatInputTextBox.HintTextProperty);
			}
			set
			{
				base.SetValue(ChatInputTextBox.HintTextProperty, value);
			}
		}
		public string Text
		{
			get
			{
				if (this.realBox == null)
				{
					return (string)base.GetValue(ChatInputTextBox.TextProperty);
				}
				return this.realBox.get_Text();
			}
			set
			{
				if (this.realBox == null)
				{
					base.SetValue(ChatInputTextBox.TextProperty, value);
					return;
				}
				this.realBox.set_Text(value);
			}
		}
		public Brush HintForeground
		{
			get
			{
				return (Brush)base.GetValue(ChatInputTextBox.HintForegroundProperty);
			}
			set
			{
				base.SetValue(ChatInputTextBox.HintForegroundProperty, value);
			}
		}
		public ChatInputTextBox()
		{
			this.InitializeComponent();
		}
		protected override void OnGotFocus(RoutedEventArgs routedEventArgs_0)
		{
			base.OnGotFocus(routedEventArgs_0);
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.hintTxt = (base.GetTemplateChild("hintTxt") as TextBlock);
			this.realBox = (base.GetTemplateChild("realBox") as TextBox);
			this.scrollViewer = (base.GetTemplateChild("scrollViewer") as ScrollViewer);
			if (this.hintTxt == null)
			{
			}
			this.realBox.set_Text(this.Text);
			if (!string.IsNullOrEmpty(this.Text))
			{
				this.realBox.set_SelectionStart(this.Text.get_Length());
				this.realBox.set_SelectionLength(0);
				this.refreshTxt();
			}
			this.realBox.add_GotFocus(new RoutedEventHandler(this.realBox_GotFocus));
			this.realBox.add_LostFocus(new RoutedEventHandler(this.realBox_LostFocus));
			this.realBox.add_TextChanged(new TextChangedEventHandler(this.realBox_TextChanged));
			this.realBox.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(this.realBox_KeyDown), true);
			this.hintTxt.add_MouseLeftButtonDown(new MouseButtonEventHandler(this.hintTxt_MouseLeftButtonDown));
		}
		private List<string> getNickNameList()
		{
			if (this.strNickNameList != null && this.strNickNameList.get_Count() > 0)
			{
				return this.strNickNameList;
			}
			if (ChatContainerPage.getCuChatContainerPage() != null)
			{
				string cuUser = ChatContainerPage.getCuChatContainerPage().mChatPageCtrl.cuUser;
				if (ContactMgr.getUserType(cuUser) == ContactMgr.UserType.UserTypeChatRoom)
				{
					this.strNickNameList = ChatRoomMgr.getChatRoomMemberNickNameList(cuUser);
				}
			}
			return this.strNickNameList;
		}
		private void realBox_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.get_Key() == 1)
			{
				this.bBackKeyDown = true;
				List<string> nickNameList = this.getNickNameList();
				string text = this.realBox.get_Text();
				string[] array = text.Split(new char[]
				{
					'@'
				});
				if (array.Length > 1 && nickNameList.Contains(array[array.Length - 1]))
				{
					this.realBox.set_Text(text.Substring(0, text.get_Length() - array[array.Length - 1].get_Length() - 1));
					this.realBox.set_SelectionStart(this.realBox.get_Text().get_Length());
					this.realBox.set_SelectionLength(0);
					return;
				}
			}
			else
			{
				this.bBackKeyDown = false;
			}
		}
		private void hintTxt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			this.FocusRealBox();
		}
		private bool isSensitiveChar(char char_0)
		{
			return (char_0 >= 'a' && char_0 <= 'z') || (char_0 >= 'A' && char_0 <= 'Z') || (char_0 >= '0' && char_0 <= '9') || char_0 == '_';
		}
		private void realBox_TextChanged(object sender, TextChangedEventArgs e)
		{
			this.refreshTxt();
			if (!string.IsNullOrEmpty(this.realBox.get_Text()) && this.bEnableAtNotify && this.realBox.get_Text().get_Length() > 0 && this.realBox.get_Text().get_Chars(this.realBox.get_Text().get_Length() - 1) == '@')
			{
				if (this.realBox.get_Text().get_Length() > 1 && this.isSensitiveChar(this.realBox.get_Text().get_Chars(this.realBox.get_Text().get_Length() - 2)))
				{
					return;
				}
				if (this.ChatTxtBoxAtMember != null && !this.bBackKeyDown)
				{
					this.ChatTxtBoxAtMember(this, null);
					this.bBackKeyDown = true;
				}
			}
		}
		public void refreshTxt()
		{
			if (!string.IsNullOrEmpty(this.realBox.get_Text()))
			{
				this.hintTxt.set_Visibility(1);
				if (this.realBox.get_Text().get_Length() - this.realBox.get_SelectionStart() < 10)
				{
					base.get_Dispatcher().BeginInvoke(delegate
					{
						this.scrollViewer.ScrollToVerticalOffset(this.realBox.get_ActualHeight());
					});
				}
				return;
			}
			if (this.isFocus && !this.mBShowHintWhenEmpty)
			{
				this.hintTxt.set_Visibility(1);
				return;
			}
			this.hintTxt.set_Visibility(0);
		}
		private void realBox_LostFocus(object sender, RoutedEventArgs e)
		{
			this.isFocus = false;
			this.refreshTxt();
		}
		private void realBox_GotFocus(object sender, RoutedEventArgs e)
		{
			this.isFocus = true;
			this.refreshTxt();
		}
		public void FocusRealBox()
		{
			try
			{
				if (this.realBox != null)
				{
					this.realBox.UpdateLayout();
					this.realBox.Focus();
				}
			}
			catch
			{
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/ChatInputTextBox.xaml", 2));
		}
	}
}
